$("#form-user-edit").validate({
	rules:{
		userName:{
			required:true,
		},
		deptName:{
			required:true,
		},
		email:{
			required:true,
            email:true,
            remote: {
                url: ctx + "system/user/checkEmailUnique",
                type: "post",
                dataType: "json",
                data: {
                	"id": function() {
                        return $("input[name='id']").val();
                    },
        			"email": function() {
                        return $("input[name='email']").val();
                    }
                },
                dataFilter: function (data, type) {
                    if (data == "0") return true;
                    else return false;
                }
            }
		},
		phone:{
			required:true,
			isPhone:true,
            remote: {
                url: ctx + "system/user/checkPhoneUnique",
                type: "post",
                dataType: "json",
                data: {
                	"id": function() {
                        return $("input[name='id']").val();
                    },
        			"phone": function() {
                        return $("input[name='phone']").val();
                    }
                },
                dataFilter: function (data, type) {
                    if (data == "0") return true;
                    else return false;
                }
            }
		},
	},
	messages: {
		"email": {
            remote: "Email已经存在"
        },
		"phone":{
        	remote: "手机号码已经存在"
		}
    },
	submitHandler:function(form){
		update();
	}
});

function update() {
    var id = $("input[name='id']").val();
    var deptId = $("input[name='deptId']").val();
    var deptName = $("input[name='deptName']").val();
    var loginName = $("input[name='loginName']").val();
    var realName = $("input[name='realName']").val();
    var password = $("input[name='password']").val();
    var email = $("input[name='email']").val();
    var phone = $("input[name='phone']").val();
    var sex = $("#sex option:selected").val();
    var userStatus = $("input[name='userStatus']").is(':checked') == true ? 0 : 1;
    var roleIds = $.getCheckeds("role");
    $.ajax({
        cache : true,
        type : "POST",
        url : ctx + "system/user/save",
        data : {
            "id": id,
            "deptId": deptId,
            "deptName": deptName,
            "loginName": loginName,
            "realName": realName,
            "password": password,
            "email": email,
            "phone": phone,
            "sex": sex,
            "userStatus": userStatus,
            "roleIds": roleIds
        },
		async : false,
		error : function(request) {
			$.modalAlert("系统错误", modal_status.FAIL);
		},
		success : function(data) {
            if (data.stat == web_status.SUCCESS) {
				parent.layer.msg("修改成功,正在刷新数据请稍后……",{icon:1,time: 500,shade: [0.1,'#fff']},function(){
					$.parentReload();
				});
			} else {
				$.modalAlert(data.message, modal_status.FAIL);
			}
		}
	});
}

/*用户管理-修改-选择部门树*/
function selectDeptTree() {
	var deptId = $("#treeId").val();
    deptId=deptId  ? "1" : deptId;
    var url = ctx + "system/dept/selectDeptTree/" + deptId;
    layer_show("选择部门", url, '380', '380');
}
